package com.ft.adbix.core.ad.manager;

import android.text.TextUtils;

import com.ft.adbix.core.listener.InterstitialListener;
import com.ft.adbix.core.listener.InterstitialListener;
import com.ft.adbix.core.response.Ad;
import com.ft.adbix.utils.AdLogUtils;

import java.util.HashMap;
import java.util.Map;

public class InterstitialAdManager {

    private Map<String, InterstitialListener> interstitialListenerMap;
    private Map<String, Ad> interstitialAdMap;

    private static final class InstanceHolder {
        static final InterstitialAdManager instance = new InterstitialAdManager();
    }

    public static InterstitialAdManager getInstance() {
        return InstanceHolder.instance;
    }

    private InterstitialAdManager() {
        interstitialListenerMap = new HashMap<>();
        interstitialAdMap = new HashMap<>();
    }

    public void saveInterstitialAdData(Ad ad) {
        AdLogUtils.d("saveInterstitialAdData = "+ad.getReqId());
        if (interstitialAdMap != null && ad != null) {
            interstitialAdMap.put(ad.getReqId(), ad);
        }
    }

    public void saveInterstitialAdListener(String reqId, InterstitialListener listener){
        if (interstitialListenerMap != null && listener != null && !TextUtils.isEmpty(reqId)) {
            interstitialListenerMap.put(reqId, listener);
        }
    }

    public void removeInterstitial(String reqId){
        if (interstitialListenerMap != null && !TextUtils.isEmpty(reqId)) {
            interstitialListenerMap.remove(reqId);
        }
        if (interstitialAdMap != null && !TextUtils.isEmpty(reqId)) {
            interstitialAdMap.remove(reqId);
        }
    }

    public InterstitialListener getInterstitialListener(String reqId) {
        try {
            if (interstitialListenerMap != null && !TextUtils.isEmpty(reqId)) {
                return interstitialListenerMap.get(reqId);
            }
        } catch (Exception ignored) {

        }
        return null;
    }


    public Ad getInterstitialAdData(String reqId) {
        try {
            if (interstitialAdMap != null && !TextUtils.isEmpty(reqId)) {
                return interstitialAdMap.get(reqId);
            }
        } catch (Exception ignored) {

        }
        return null;
    }

}
